import { $http } from '@escook/request-miniprogram'
wx.$http = $http
$http.baseUrl = 'https://live-api.itheima.net'

//请求拦截器
$http.beforeRequest = function (config) {
  if (wx.getStorageSync('token')) {
    config.header.Authorization = `Bearer ${wx.getStorageSync('token')}`
  }
}

//响应拦截器
$http.afterRequest = function (response) {
  if (response.statusCode === 500 || response.statusCode === 401) {
    wx.Toast.fail('登录过期！');
    wx.setStorageSync('userToken', null)
    setTimeout(function () {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }, 1000)
  } else if (response.statusCode !== 200) {
    wx.Toast.fail('异常错误！');
  }
  return
}